package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MouldCheckItemFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.MouldCheckItemMapper;
import com.iemes.mapper.mould.MouldCheckStandardMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;


@RequestMapping("/zInstanCheckItem/")
@Controller
public class ZInstallcheckItemController extends BaseController {

	@Inject
	private MouldCheckItemMapper mouldCheckItemMapper;
	
	@Inject
	private MouldCheckStandardMapper mouldCheckStandardMapper;
	
	@RequestMapping("examineOrder")
	public String examineOrder(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/zInstallcheckItem/zInstallcheckItem_list";
	}
	
	@ResponseBody
	@RequestMapping("pCheckItem_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		MouldCheckItemFormMap mouldCheckItemFormMap = getFormMap(MouldCheckItemFormMap.class);
		//限定formCategory为4
		mouldCheckItemFormMap.put("formCategory", 4);
		//限定status为1
		mouldCheckItemFormMap.put("status", 1);
		List<Map<String, Object>> list=mouldCheckItemMapper.getAll(mouldCheckItemFormMap);
		LuogicPaging(pageNow, pageSize, list);
		return pageView;
	}
	
	@RequestMapping("pCheckItem_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("pCheckItem", baseMapper.findbyFrist("id", id, MouldCheckItemFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/mould/zInstallcheckItem/zInstallcheckItem_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="pCheckItem_editEntity")
	@SystemLog(module="备模前模具质量检查",methods="编辑检查项目-修改检查项目")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		MouldCheckItemFormMap mouldCheckItemFormMap = getFormMap(MouldCheckItemFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		mouldCheckItemFormMap.put("date", sdf.format(date));
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		mouldCheckItemFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		mouldCheckItemMapper.editEntity(mouldCheckItemFormMap);
		return "success";
	}
	
	@RequestMapping("pCheckItem_add")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/mould/zInstallcheckItem/zInstallcheckItem_add";
	}
	
	@ResponseBody
	@RequestMapping("pCheckItem_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="备模前模具质量检查",methods="编辑检查项目-新增检查项目")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		MouldCheckItemFormMap mouldCheckItemFormMap = getFormMap(MouldCheckItemFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		mouldCheckItemFormMap.put("date", sdf.format(date));
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		mouldCheckItemFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		mouldCheckItemMapper.addEntity(mouldCheckItemFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("pCheckItem_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="备模前模具质量检查",methods="编辑检查标准-删除检查标准")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		MouldCheckItemFormMap mouldCheckItemFormMap=getFormMap(MouldCheckItemFormMap.class);
		MouldCheckStandardFormMap mouldCheckStandardFormMap=getFormMap(MouldCheckStandardFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		for(int i=0;i<str.length;i++){
			mouldCheckItemFormMap=baseMapper.findbyFrist("id", str[i], MouldCheckItemFormMap.class);
			mouldCheckItemFormMap.put("date", sdf.format(date));
			mouldCheckItemFormMap.put("fillPeopleId", userFormMap.getInt("id"));
			mouldCheckItemFormMap.set("status", 0);
			mouldCheckItemFormMap.put("id", str[i]);
			baseMapper.editEntity(mouldCheckItemFormMap);
			List<MouldCheckStandardFormMap> standard=mouldCheckStandardMapper.getByItemId(Integer.parseInt(str[i]));
			for(MouldCheckStandardFormMap m:standard){
				mouldCheckStandardFormMap.put("date", sdf.format(date));
				mouldCheckStandardFormMap.put("fillPeopleId", userFormMap.getInt("id"));
				mouldCheckStandardFormMap.set("status", 0);
				mouldCheckStandardFormMap.put("id", m.getInt("id"));
				baseMapper.editEntity(mouldCheckStandardFormMap);
			}
		}
		return "success";
	}
}